<?php
/**
 * 公用方法
 */

include_once 'inc/utility_all.php';
include_once 'inc/utility_sms1.php';
include_once 'inc/utility_msg.php';
include_once '../mysqlDB2.class.php';
include_once 'common.ser.php';
class travelSer{
    private $db;
    public function __construct(){
        $this->db = new mysqlDB();
        $this->commSer = new commonSer();
    }
    /**
     * 检查填写状态
     * @return boolean
     */
    public function checkChatPack(){
        $time = date('Y-m-d',time());
        # 用当前时间查询 travel_apply_customer数据表  查询 月度出差单号 和 出差单id
        $result = $this->db->select_raw('dev_travel_apply_customer', 'apply_id,item_id', 'unix_timestamp(end_time)="'.strtotime($time).'"');
        foreach($result as $key => $value){
            # 根据获取到的item_id 获取是否填写过洽谈单
            $chat = $this->db->select_raw('dev_travel_chat', '*', 'apply_num="'.$value['item_id'].'"');
            if(empty($chat)){  # 若未填写过 则执行推送
                # 用出差单id 和 月度出差单号查询 travel_apply 数据表  获得 创建人 和 部门审批人 id 用户消息推送
                $apply = $this->db->select_raw('dev_travel_apply', 'creator_id,to_uid_step',"id = '{$value['apply_id']}'");
                # 查询 travel_chat_status
                $status = $this->db->select_raw('dev_travel_chat_status', '*', "apply_num_c='{$value["item_id"]}' and chat_id = '{$value["apply_id"]}'");
                if(!empty($status)){
                    if($status['0']['check_num'] == '3'){
                        $data['apply_user'] = $this->commSer->getUserIdByUid($apply['0']["creator_id"]);
                        $data['apply_uid'] = $apply['0']["creator_id"];
                        $data['check_num'] = '3';
                        $data['final_status'] = iconv('UTF-8', 'GBK',"无效");
                        $data['create_time_c'] = date('Y-m-d H:i:s',time());
                        $this->db->update('dev_travel_chat_status',$data,"id_c='{$status["0"]["id_c"]}'");
                    }else{
                        $data['chat_id'] = $value['apply_id'];
                        $data['apply_num_c'] = $value['item_id'];
                        $data['apply_user'] = $this->commSer->getUserIdByUid($apply['0']["creator_id"]);
                        $data['apply_uid'] = $apply['0']["creator_id"];
                        $data['check_num'] = $status['0']['check_num'] + 1;
                        $data['create_time_c'] = date('Y-m-d H:i:s',time());
                        $this->db->update('dev_travel_chat_status', $data, "id_c='{$status["0"]["id_c"]}'");
                        $TO_ID = $this->commSer->getUserIdByUid($data['apply_uid']);
                        $TO_OTHER_ID = $this->commSer->getUserIdByUid($apply['0']["to_uid_step"]);
                        $CONTENT = iconv('UTF-8', 'GBK',"您的月度出差单 ".$value['item_id']." 还未填写会议洽谈单, 请于1-3个工作日内填写完");
                        $CONTENT_OTHER = iconv('UTF-8', 'GBK',"月度出差单 ".$value['item_id']." 还未填写会议洽谈单, 请及时跟进");
                        $REMIND_URL = "1:erp5/public/tripapi/negotiation/index/";
                        send_sms(date('Y-m-d H:i:s'),"admin",$TO_ID,7,$CONTENT,$REMIND_URL);
                        send_sms(date('Y-m-d H:i:s'),"admin",$TO_OTHER_ID,7,$CONTENT_OTHER,$REMIND_URL);
                    }
                }else{
                    $data['id_c'] = NULL;
                    $data['chat_id'] = $value['apply_id'];
                    $data['apply_num_c'] = $value['item_id'];
                    $data['apply_user'] = $this->commSer->getUserIdByUid($apply['0']["creator_id"]);
                    $data['apply_uid'] = $apply['0']["creator_id"];
                    $data['check_num'] = 1;
                    $data['final_status'] = iconv('UTF-8', 'GBK',"有效");
                    $data['create_time_c'] = date('Y-m-d H:i:s',time());
                    $this->db->insert('dev_travel_chat_status', $data);
                    $TO_ID = $this->commSer->getUserIdByUid($data['apply_uid']);
                    $TO_OTHER_ID = $this->commSer->getUserIdByUid($apply['0']["to_uid_step"]);
                    $CONTENT = iconv('UTF-8', 'GBK',"您的月度出差单 ".$value['item_id']." 还未填写会议洽谈单, 请于1-3个工作日内填写完");
                    $CONTENT_OTHER = iconv('UTF-8', 'GBK',"月度出差单 ".$value['item_id']." 还未填写会议洽谈单, 请及时跟进");
                    $REMIND_URL = "1:erp5/public/tripapi/negotiation/index/";
                    send_sms(date('Y-m-d H:i:s'),"admin",$TO_ID,7,$CONTENT,$REMIND_URL);
                    send_sms(date('Y-m-d H:i:s'),"admin",$TO_OTHER_ID,7,$CONTENT_OTHER,$REMIND_URL);
                }
            }
        }
    }
}
?>